$(function () {
    var queryUrl = '../plandracheck/list';
    var Id = getQueryString('Id');
    if(Id){
        queryUrl += '?Id=' + Id;
    }
    $("#jqGrid").Grid({
        url: queryUrl,
        colModel: [
            {label: '申请人', name: 'applyName', index: 'APPLY_USER', width: 80},
            {label: '申请单位', name: 'deptName', index: 'APPLY_DEPT', width: 80},
            {
                label: '申请时间', name: 'applyDate', index: 'APPLY_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: '材料类型', name: 'matType', index: 'MAT_TYPE', width: 80},
            {label: '零件数量', name: 'partNum', index: 'PART_NUM', width: 80},
            {
                label: '需求等级', name: 'draLevel', index: 'DRA_LEVEL', width: 80,
                formatter: function (value) {
                    return 1 == value ? '紧急' : '一般';
                }
            },
            {label: '用途', name: 'purpose', index: 'PURPOSE', width: 80},
            {label: '备注', name: 'remark', index: 'REMARK', width: 80},
            {label: '车间室主任', name: 'deptCheckName', index: 'DEPT_CHECK_USER', width: 80},
            {name: 'deptCheckStatus',hidden: true},
            {label: '审批时间',hidden: true, name: 'deptCheckDate', index: 'DEPT_CHECK_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {label: 'PE审批人',hidden: true, name: 'peCheckName'},
            {name: 'peCheckStatus',hidden: true},
            {label: '审批时间',hidden: true,name: 'peCheckDate', index: 'PE_CHECK_DATE', width: 80,
                formatter: function (value) {
                    return transDate(value, "yyyy-MM-dd");
                }
            },
            {
                label: '审批详情', width: 80, align: 'center', sortable: false,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' onclick='vm.shviewOption(\"" + escape(JSON.stringify(row)) + "\")'>&nbsp;详情</button>";
                }
            },
            {
                label: '附件', width: 80, align: 'center', sortable: false,
                formatter: function (value, col, row) {
                    return "<button class='btn btn-outline btn-info' onclick='vm.goPlanDraFile(\"" + row.id + "\")'><i class='fa fa-upload'></i>&nbsp;上传</button>";
                }
            },
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        planDraCheck: {
            updateDate: '',
            updateUser: '',
            createDate: '',
            createUser: '',
            remark: '',
            peCheckDate: '',
            peCheckStatus: '',
            peCheckUser: '',
            deptCheckDate: '',
            deptCheckStatus: '',
            deptCheckUser: '',
            delFlag: '',
            purpose: '',
            draLevel: '',
            partNum: '',
            matType: '',
            applyDate: '',
            applyDept: '',
            applyUser: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        },
    },
    methods: {
        //审核详情
        shviewOption: function (res) {
            let data = JSON.parse(unescape(res))
            if(!data.okUserName){data.okUserName="-"}
            let dom = "<div class='details' >" +
                "<table>" +
                "<tr><td>部门审批人：</td><td>"+data.deptCheckName+"</td></tr>" +
                "<tr><td>审批状态：</td><td>"+sysEnums.CHECK_FLAG[data.deptCheckStatus]+"</td></tr>" +
                "<tr><td>审批时间：</td><td>"+transDate(data.deptCheckDate, "yyyy-MM-dd hh:mm")+"</td></tr>" +
                "<tr><td>PE审批人：</td><td>"+data.peCheckName+"</td></tr>" +
                "<tr><td>审批状态：</td><td>"+sysEnums.CHECK_FLAG[data.peCheckStatus]+"</td></tr>" +
                "<tr><td>审批时间：</td><td>"+transDate(data.peCheckDate, "yyyy-MM-dd hh:mm")+"</td></tr>" +
                "</table></div>"
            layer.open({
                type: 1,
                title: "详情",
                closeBtn: 0,
                area: ['450px'],
                offset: '10vh',
                shadeClose: true,
                skin: 'yourclass',
                content: dom
            });
        },
        deptCheck: function () {
            var data = getSelectedRowData("#jqGrid");

            if (data.id == null) {
                return;
            }

            if (data.deptCheckStatus !== '未审核') {
                alert("此记录已审核", function () {
                    vm.reload()
                });
                return;
            }

            openWindow({
                title: "确认审核",
                area: ['350px', '200px'],
                content: jQuery("#reviewLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    if (typeof (vm.planDraCheck.checkResult) == 'undefined') {
                        alert("请选择审核状态");
                        return;
                    }
                    vm.planDraCheck = {deptCheckStatus: vm.planDraCheck.checkResult, id: data.id};
                    var url = '../plandracheck/deptCheck';
                    var params = JSON.stringify(vm.planDraCheck);
                    vm.updateCheck(url, params)
                }
            });
        },
        peCheck: function () {
            var data = getSelectedRowData("#jqGrid");
            if (data.id == null) {
                return;
            }
            if (data.deptCheckStatus != '1') {
                alert("此记录未通过车间室主任审核", function () {
                    vm.reload()
                });
                return;
            }
            if (data.peCheckStatus != '0') {
                alert("此记录已审核", function () {
                    vm.reload()
                });
                return;
            }
            openWindow({
                title: "确认审核",
                area: ['350px', '200px'],
                content: jQuery("#reviewLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    if (typeof (vm.planDraCheck.checkResult) == 'undefined') {
                        alert("请选择审核状态");
                        return;
                    }
                    vm.planDraCheck = {peCheckStatus: vm.planDraCheck.checkResult, id: data.id};
                    var url = '../plandracheck/peCheck';
                    var params = JSON.stringify(vm.planDraCheck);
                    vm.updateCheck(url, params)
                }
            });
        },
        updateCheck: function (url, params) {
            Ajax.request({
                url: url,
                params: params,
                contentType: "application/json",
                type: 'POST',
                successCallback: function (res) {
                    alert('审核成功!');
                    setTimeout(function () {
                        location.reload();
                    }, 1000);
                }
            });
        },
        goPlanDraFile: function (id) {
            openWindow({
                type: 2,
                title: '<i class="fa fa-upload"></i> 上传附件',
                content: '../sys/plandrafile.html?id=' + id,
                cancel: function (index, layero) {
                    layer.close(index);
                    vm.reload();
                }
            });
        },
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.planDraCheck = {
                updateDate: '',
                updateUser: '',
                createDate: '',
                createUser: '',
                remark: '',
                peCheckDate: '',
                peCheckStatus: '',
                peCheckUser: '',
                deptCheckDate: '',
                deptCheckStatus: '',
                deptCheckUser: '',
                delFlag: '',
                purpose: '',
                draLevel: 1,
                partNum: '',
                matType: '',
                applyDate: '',
                applyDept: '',
                applyUser: '',
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.planDraCheck.id == null ? "../plandracheck/save" : "../plandracheck/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.planDraCheck),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../plandracheck/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../plandracheck/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.planDraCheck = r.planDraCheck;
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});
